package com.ruoyi.project.hcy.saleManagement.service;

import com.ruoyi.common.constant.checkNameUniqueContent;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.hcy.saleManagement.domain.*;
import com.ruoyi.project.hcy.saleManagement.mapper.OrderManagerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class OrderManagerServiceImp implements IOrderManagerService {
    @Autowired
    OrderManagerMapper OrderManagerMapper;


    @Override
    public List<ProjectVo> listFun(Project project) {
        /*
        1.先查询   crew 与 dept 的表连接（T1），再查 client 与 project 表的连接 (T2), 然后 两个查询结果， 根据
            T1 的crewID 和   T2的crewID  形成 on 的关系
        2，我们需要project表的详细数据，所以使用右连接
         */
        List<ProjectVo> list = OrderManagerMapper.listFun(project);
        list.forEach(s -> System.out.println(s));
        return list;
    }

    @Override
    public List<Order> selectProjectTypeName() {
        List<Order> list = OrderManagerMapper.selectProjectTypeName();
        System.out.println(list);
        return list;
    }

    @Override
    public String checkTypeNameUnique(Vo vo) {
        Integer id = null;
        if (vo.getClientId() != null) {
            String ClientId = OrderManagerMapper.checkClientId(Integer.parseInt(vo.getClientId()));
            return StringUtils.isNull(ClientId) ? checkNameUniqueContent.TYPE_NAME_UNIQUE : checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;
        } else if (vo.getCrewName() != null) {
            String client = OrderManagerMapper.checkCrewName(vo.getCrewName());
            return StringUtils.isNull(client) ? checkNameUniqueContent.TYPE_NAME_UNIQUE : checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;
        } else if (vo.getDeptPrincipal() != null) {
            String deptName = OrderManagerMapper.checkDeptPrincipal(vo.getDeptPrincipal());
            return StringUtils.isNull(deptName) ? checkNameUniqueContent.TYPE_NAME_UNIQUE : checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;
        } else if (vo.getProjectId() != null) {
            Project info = OrderManagerMapper.checkProjectId(Integer.parseInt(vo.getProjectId()));
            return StringUtils.isNull(info) ? checkNameUniqueContent.TYPE_NAME_UNIQUE : checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;
        } else if (vo.getProjectTitle() != null) {
            Project info = OrderManagerMapper.checkProjectTitle(vo.getProjectTitle());
            return StringUtils.isNull(info) ? checkNameUniqueContent.TYPE_NAME_UNIQUE : checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;
        } else if (vo.getScheduleId() != null) {
            String scheduleId = OrderManagerMapper.checkScheduleId(Integer.parseInt(vo.getScheduleId()));
            return StringUtils.isNull(scheduleId) ? checkNameUniqueContent.TYPE_NAME_UNIQUE : checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;
        } else {
            return checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;
        }
    }

    @Override
    public int addSaveFun(Client client, ProjectVo projectVo, Schedule schedule,String crewName) {



        /*先添加client*/
//        OrderManagerMapper.addSaveFun(client);
        int i1 = OrderManagerMapper.addSaveFun(client);

        /*project虚要crew的ID，根据crewName查询*/

       Integer CrewId =  OrderManagerMapper.selectCrewName(crewName);
        projectVo.setCrewId(CrewId);

        /*2，添加 project*/
        int i2 = OrderManagerMapper.addSaveProjectFun(projectVo);
        /*3。添加schdule*/
        int i3 = OrderManagerMapper.addSaveSchduleFun(schedule);

        /*返回1 则不成功， 返回0 则成功*/
        return i1 > 0 && i2 > 0 && i3 > 0 ? 1 : 0;
    }

    @Override
    public String checkCrewName(String crewName, String deptName) {
        Integer exitCrew = OrderManagerMapper.checkCrewNameCount(crewName,deptName);
        System.out.println(exitCrew);
        /*
        exitCrew>0
        ：Crew员工存在（>0），放行，不存在(<0)，不放行
        返回1，不放行
        页面效果：返回0 放行，返回1 不放行 */
        return exitCrew > 0 ? checkNameUniqueContent.TYPE_NAME_UNIQUE :checkNameUniqueContent. TYPE_NAME_NOT_UNIQUE;
    }

    @Override
    public String checkdeptPrincipal(String deptPrincipal,String deptName) {
        /*传入主理人名字，判断主理人是否是存在，与销售部门中*/
        Integer exitPrincipal  =OrderManagerMapper.checkdeptPrincipal(deptPrincipal, deptName);
        System.out.println("是否存在："+exitPrincipal);
        /*判断是否有该主理人，并且是否属于该销售部门， 是：返回0 放行   不是：返回 1 不放行*/
        return exitPrincipal > 0 ? checkNameUniqueContent.TYPE_NAME_UNIQUE :checkNameUniqueContent.TYPE_NAME_NOT_UNIQUE;
    }

    @Override
    public ProjectVo selectProjectWithCrewAndClientAndSchedule(int id) {
        ProjectVo projectVo = OrderManagerMapper.selectProjectWithCrewAndClientAndSchedule(id);
        projectVo.setLocaldateTimeString(projectVo.getLocaldateTimeString().replace(" ", "T"));
        return projectVo;
    }

    @Override
    public boolean editSave(ProjectVo projectVo) {
        /*没有crewID,需要通过名字查询，在sql中操作*/
        System.out.println(projectVo.getCrewName());
        int i =  OrderManagerMapper.editSave(projectVo);
        return i>0?true :false;
    }

    @Override
    public String getDeptName() {
      return   OrderManagerMapper.getDeptName();

    }

    @Override
    public boolean deleteProjects(Integer[] idss) {
        int y  = 0;
        List<Integer> ids = Arrays.asList(idss);
        y =  OrderManagerMapper.deleteClient(ids);
        y =  OrderManagerMapper.deleteSchedule(ids);
        y = OrderManagerMapper.deleteProjects(ids);
        return  y>0?true :false;
    }
}
